home *** CD-ROM | disk | FTP | other *** search
/ Visual Cafe 3 / Visual Cafe 3.ISO / Vcafe / JFC.bin / StringSpinner.java < prev    next >
Text File  |  1998-06-30  |  6KB  |  207 lines

  1. /*
  2.  * @(#)StringSpinner.java    1.13 02/16/98
  3.  * 
  4.  * Copyright (c) 1997 Sun Microsystems, Inc. All Rights Reserved.
  5.  * 
  6.  * This software is the confidential and proprietary information of Sun
  7.  * Microsystems, Inc. ("Confidential Information").  You shall not
  8.  * disclose such Confidential Information and shall use it only in
  9.  * accordance with the terms of the license agreement you entered into
  10.  * with Sun.
  11.  * 
  12.  * SUN MAKES NO REPRESENTATIONS OR WARRANTIES ABOUT THE SUITABILITY OF THE
  13.  * SOFTWARE, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
  14.  * IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
  15.  * PURPOSE, OR NON-INFRINGEMENT. SUN SHALL NOT BE LIABLE FOR ANY DAMAGES
  16.  * SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING OR DISTRIBUTING
  17.  * THIS SOFTWARE OR ITS DERIVATIVES.
  18.  * 
  19.  */
  20.  
  21. package com.sun.java.swing.plaf.basic;
  22.  
  23. import java.awt.*;
  24. import java.awt.event.*;
  25. import java.io.*;
  26.  
  27.  
  28. /**
  29.  * A typein field for an integer, where the integers are named.
  30.  * <p>
  31.  * Warning: serialized objects of this class will not be compatible with
  32.  * future swing releases.  The current serialization support is appropriate
  33.  * for short term storage or RMI between Swing1.0 applications.  It will
  34.  * not be possible to load serialized Swing1.0 objects with future releases
  35.  * of Swing.  The JDK1.2 release of Swing will be the compatibility
  36.  * baseline for the serialized form of Swing objects.
  37.  * <p>
  38.  * For the keyboard keys used by this component in the standard Look and
  39.  * Feel (L&F) renditions, see the
  40.  * <a href="doc-files/Key-Index.html#JStringSpinner">JStringSpinner</a> key assignments.
  41.  * <b>Note:</b> The link above is only a placeholder. It will not be valid
  42.  *              until this component is released.
  43.  *
  44.  * @author James Gosling
  45.  */
  46. public class StringSpinner extends Spinner {
  47.     private String names[];
  48.     private String typed = null;
  49.  
  50.     public StringSpinner(int init, String t, String[] names) {
  51.         super(init, t);
  52.         this.names = names;
  53.         setMinimum(0);
  54.         setMaximum(names.length-1);
  55.         setWrap(true);
  56.         updateUI();
  57.     }
  58.  
  59. //     public Dimension getMinimumSize() {
  60. //         if (d == null) {
  61. //             fm = getFontMetrics(getFont());
  62. //             int w = 0;
  63. //             for (int i = names.length; --i>=0; ) {
  64. //                 int tw = fm.stringWidth(names[i]);
  65. //                 if (tw>w) w = tw;
  66. //             }
  67. //             if (txt!=null) w+=fm.stringWidth(txt);
  68. //             d = new Dimension(w+8, fm.getHeight()+8);
  69. //             ascent = fm.getAscent();
  70. //         }
  71. //         return d;
  72. //     }
  73.     public String[] getNameArray() {return names;}
  74.  
  75.     public String getValueName() {return names[value];}
  76.  
  77.     public String getTypedString() {return typed;}
  78.  
  79.  
  80. //     protected void  processKeyEvent(KeyEvent e){
  81. //         if (e.getID() != e.KEY_PRESSED) return;
  82. //         if (e.isActionKey())
  83. //             switch(e.getKeyCode()) {
  84. //              case e.VK_DOWN:    setValue(value-1); break;
  85. //                 case e.VK_UP:      setValue(value+1); break;
  86. //             }
  87. //         else {
  88. //             int c = e.getKeyChar();
  89. //             if (040<c && c<0177) {
  90. //                 typed = typed != null ? typed + (char) c
  91. //                     : String.valueOf((char)c);
  92. //                 resynctyped();
  93. //             }
  94. //             else switch(c) {
  95. //                 case 0177:
  96. //                 case '\b':
  97. //                     if (typed!=null)
  98. //                         if (typed.length()<=0) typed = null;
  99. //                         else typed = typed.substring(0,typed.length()-1);
  100. //                     resynctyped();
  101. //                     break;
  102. //             }
  103. //         }
  104. //         return;
  105. //     }
  106.     private void resynctyped() {
  107.         if (typed==null) {
  108.           //setValue(0);
  109.           //repaint();
  110.         } else {
  111.             int len = typed.length();
  112.             for(int i = 0; i<names.length; i++)
  113.                 if (typed.regionMatches(true,0,names[i],0,len)) {
  114.                     setValue(i);
  115.                     repaint();
  116.                     return;
  117.                 }
  118.             if (len>1) typed = typed.substring(0,len-1);
  119.             else typed = null;
  120.             resynctyped();
  121.         }
  122.     }
  123. //     public void paint(Graphics g) {
  124. //         if (d == null) getMinimumSize(); //Force computation of ascent
  125.  
  126. //      g.setColor(Color.white);
  127. //      g.drawRect(0,0,d.width-1,d.height-1);
  128. //      g.setColor(Color.black);
  129. //      g.drawLine(0,0,0,d.height-1);
  130. //      g.drawLine(1,0,1,d.height-3); 
  131. //      g.drawLine(1,0,d.width,0);
  132. //      g.drawLine(1,1,d.width-2,1);
  133. //      g.setColor(Color.white);
  134. //      g.fillRect(3,3,d.width-5,d.height-5);
  135.  
  136.  
  137. //      if (haveFocus) 
  138. //        {
  139. //          g.setColor(new Color(0,0,150));
  140. //          g.fillRect(4, 4, d.width-7, d.height-7);
  141. //          g.setColor(Color.white);
  142. //        }
  143. //      else
  144. //        {
  145. //          g.setColor(Color.black);
  146. //        }
  147. //      String s = names[value];
  148. //      if (txt != null) s += txt;
  149. //         if (typed==null)
  150. //        {
  151. //          g.drawString(s, 4, ascent + 4);
  152. //        }
  153. //      else 
  154. //        {
  155. //             g.setColor(Color.lightGray);
  156. //             g.drawString(s, 4, ascent+4);
  157. //             g.setColor(Color.white);
  158. //             g.drawString(s.substring(0,typed.length()), 4, ascent+4);
  159. //        }
  160. //     }
  161.  
  162. //     protected void processFocusEvent(FocusEvent e){
  163. //         typed = null;
  164. //         super.processFocusEvent(e);
  165. //     }
  166.  
  167.   public void keyPressed(KeyEvent e)
  168.   {
  169.     if (e.isActionKey())
  170.       switch(e.getKeyCode()) {
  171.       case e.VK_DOWN:    setValue(value-1); break;
  172.       case e.VK_UP:      setValue(value+1); break;
  173.       }
  174.     else {
  175.       int c = e.getKeyChar();
  176.       if (040<c && c<0177) {
  177.         typed = typed != null ? typed + (char) c
  178.           : String.valueOf((char)c);
  179.         resynctyped();
  180.       }
  181.       else switch(c) {
  182.       case 0177:
  183.       case '\b':
  184.         if (typed!=null)
  185.           if (typed.length()<=0) typed = null;
  186.           else typed = typed.substring(0,typed.length()-1);
  187.         resynctyped();
  188.         break;
  189.       }
  190.     }
  191.     return;
  192.   }
  193.  
  194.   public void focusGained(FocusEvent e) 
  195.   {
  196.     typed = null;
  197.     repaint();
  198.     super.focusGained(e);
  199.   }
  200.   public void focusLost(FocusEvent e)
  201.   {
  202.     typed = null;
  203.     repaint();
  204.     super.focusLost(e);
  205.   }
  206. }
  207.